import {Component} from '@angular/core'
// import {MyApp} from "../../../app/app.component";
import { Camera, CameraOptions } from '@ionic-native/camera';

@Component({
  selector: 'page-native-camera',
  templateUrl: 'camera.html'
})
export class NativeCameraPage {

  image:any

  constructor(public camera: Camera) {
    this.getPicture = this.getPicture.bind(this)
    this.getPictureSave = this.getPictureSave.bind(this)
    this.getPhotoLibrary = this.getPhotoLibrary.bind(this)
    this.getPictureSelf = this.getPictureSelf.bind(this)
  }

  getPicture(options:CameraOptions = {
      quality: 100,
      destinationType:this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
  }) {
    this.camera.getPicture(options).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64:
      this.image = 'data:image/jpeg;base64,' + imageData;
      // this.image = imageData;
    }, (err) => {
      console.log(err)
      // Handle error
    });
  }

  getPictureSave() {
    this.getPicture({
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum:true
    })
  }

  getPhotoLibrary(){
    this.getPicture({
      sourceType:this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType:this.camera.DestinationType.DATA_URL,
      allowEdit:true
    })
  }

  getPictureSelf(){
    this.getPicture({
      destinationType:this.camera.DestinationType.DATA_URL,
      cameraDirection: this.camera.Direction.FRONT,
    })
  }

}
